<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/5/14
 * Time: 0:21
 */
include "../classes/logincheck.php";
include '../classes/conn.php';
if(islogin()!='teacher_login'){
    die('error');
}


$class=$_POST['class'];
$sec_num=$_POST['sec_num'];
$sec_name=$_POST['sec_name'];


$query1="select num from section where num='{$sec_num}'";
$res=mysql_query($query1);
if(mysql_num_rows($res)>0){
    die('sec_exist');
}
$query2="insert into section (num,name,class,teacher) values ('{$sec_num}','{$sec_name}','{$class}','{$_SESSION['t_num']}')";
mysql_query($query2);


if(isset($_POST['sub_num'])&&isset($_POST['sub_name'])) {
    $sub_num = $_POST['sub_num'];
    $sub_name = $_POST['sub_name'];
    for ($i = 0; $i < count($sub_num); $i++) {
        if ($sub_num[$i] != "") {
//        查询小节序号是否存在
            $query = "select num from subsection where num='{$sub_num[$i]}' and section='{$sec_num}' and class='{$class}' and teacher='{$_SESSION['t_num']}'";
            $res = mysql_query($query);
            if (mysql_num_rows($res)) {
                echo 'sub_exist';
            } else {
//        插入数据
                $query = "insert into subsection (num,name,section,class,teacher) values ('{$sub_num[$i]}','{$sub_name[$i]}','{$sec_num}','{$class}','{$_SESSION['t_num']}')";
                mysql_query($query);
            }
        }
    }
}
else die('no_sub');
